package com.backend.demo1.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.backend.demo1.config.ApiResponse;
import com.backend.demo1.services.UploadService;

import org.springframework.beans.factory.annotation.Autowired;

@RestController
@RequestMapping("/api/upload")
public class UploadController {

    @Autowired 
    private UploadService uploadService;

    @PostMapping("/image")
    public ResponseEntity<ApiResponse<String>> uploadImage(@RequestParam("albumImage") MultipartFile file) {
        try {
            // 调用服务层保存图片并获取访问路径
            String imagePath = uploadService.saveImage(file);
            return ResponseEntity.ok(ApiResponse.success(imagePath));
        } catch (Exception e) {
            return ResponseEntity.status(500).body(ApiResponse.error(500, "图片上传失败: " + e.getMessage()));
        }
    }
}